<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_student extends CI_Model {
    /**
     * 继承父类
     */
    function __construct(){
        parent::__construct();
    }
    /**
     * 添加学生信息
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    public function student_add($str){
        $into='';
        $k='';
        foreach ($str as $key => $value) {
            $k.=$k?(" , ".$key):$key;
            $into.=$into?(" , '".$value."'"):("'".$value."'");
        }
        $query="INSERT INTO stu_student (".$k.") VALUES (".$into.")";
        return $this->db->query($query);
    }
    /**
     * 直接查找学生信息
     */
    public function student_search($str=array()){
        $where='';
        foreach ($str as $key => $value) {
            $where.=$where?(" AND ".$key."='".$value."'"):($key."='".$value."'");
        }
        $query="SELECT * FROM stu_student WHERE ".$where."ORDER BY s_id  DESC";
        $result=$this->db->query($query);
        return $result->result();
    }
    /**
     * 通过学号查询学生信息
     * @param  [type] $params [description]
     * @return [type]         [description]
     */
    public function student_login_search($params){
        $query="SELECT * FROM stu_s_login INNER JOIN stu_student ON stu_s_login.s_id=stu_student.s_id WHERE stu_s_login.no='".$params."'";
        $result=$this->db->query($query);
        return $result->result();
    }
    /**
     * 添加学生登录信息
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    public function student_login_add($str){
        $into='';
        $k='';
        foreach ($str as $key => $value) {
            $k.=$k?(" , ".$key):$key;
            $into.=$into?(" , '".$value."'"):("'".$value."'");
        }
        $query="INSERT INTO stu_s_login (".$k.") VALUES (".$into.")";
        return $this->db->query($query);
    }
    /**
     * 跟新学生信息
     * @param  [type] $str1 [description]
     * @param  [type] $str2 [description]
     * @return [type]       [description]
     */
    public function student_update($str1,$str2){
        $str='';
        $where='';
        foreach ($str1 as $key => $value) {
            if($key){
                $str=$str?($str." , "):$str;
                $str=$str.$key."='".$value."'";
            }
        }
        foreach ($str2 as $key => $value) {
            if($key){
                $where=$where?($where."AND"):$where;
                $where=$where.$key."='".$value."'";
            }
        }
        $query="UPDATE stu_student SET ".$str." WHERE ".$where;
        return $this->db->query($query);
    }
    /**
     * 跟新学生登录信息
     * @param  [type] $str1 [description]
     * @param  [type] $str2 [description]
     * @return [type]       [description]
     */
    public function student_login_update($str1,$str2){
        $str='';
        $where='';
        foreach ($str1 as $key => $value) {
            if($key){
                $str=$str?($str." , "):$str;
                $str=$str.$key."='".$value."'";
            }
        }
        foreach ($str2 as $key => $value) {
            if($key){
                $where=$where?($where."AND"):$where;
                $where=$where.$key."='".$value."'";
            }
        }
        $query="UPDATE stu_s_login SET ".$str." WHERE ".$where;
        return $this->db->query($query);
    }
    /**
     * 查询学生成绩单
     * @param  [type] $condition [description]
     * @return [type]            [description]
     */
    public function student_transcript_search($condition){
        $where='';
        foreach ($condition as $key => $value) {
            if($value){
                $where.=$where?(" AND ".$key."='".$value."'"):($key."='".$value."'");
            }
        }
        $query="SELECT * FROM stu_transcript WHERE ".$where." ORDER BY tr_id";
        $result=$this->db->query($query);
        return $result->result();
    }
    /**
     * 跟新成绩单
     * @param  [type] $str       [description]
     * @param  [type] $condition [description]
     * @return [type]            [description]
     */
    public function student_transcript_update($str,$condition){
        $where='';
        foreach ($condition as $key => $value) {
            if($value){
                $where.=$where?(" AND ".$key."='".$value."'"):($key."='".$value."'");
            }
        }
        $set='';
        foreach ($str as $key => $value) {
            if($value){
                $set.=$set?(" , ".$key."='".$value."'"):($key."='".$value."'");
            }
        }
        $query="UPDATE stu_transcript SET ".$set." WHERE ".$where;
        return $this->db->query($query);
    }
    /**
     * 添加学生成绩
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    public function student_transcript_insert($str){
        $keys='';
        $into='';
        foreach ($str as $key => $value) {
            if($value){
                $keys.=$keys?(" , ".$key):$key;
                $into.=$into?(" , '".$value."'"):("'".$value."'");
            }
        }
        $query="INSERT INTO stu_transcript (".$keys.") VALUES(".$into.")";
        return $this->db->query($query);
    }
}